
var time = require("../../utils/util.js");
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    codetime: '获取验证码',
    name: '',
    phone: '',
    code: '',
    num: 120,
  },
  getcode(){
    var that = this;
    // console.log(that.data.phone)   
    if(that.data.codetime == '获取验证码'){
      if(that.data.phone == ''){
        wx.showToast({
          title: '请填写手机号',
          icon: 'none',
          duration: 3000
        })
      }else{
        wx.request({
          //获取openid接口  
          url: time.httpurl + '/api/getPhoneVerification',
          data:{
            "phone": that.data.phone
          },
          method: 'POST',
          success: function (res) {
            console.log(res);
            if(res.data.code == 200){      
              that.timetid();                    
            }else{
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
                duration: 3000
              })
            }
          }
        })
      }
    }      
  },
  timetid: function(){
    var that = this;
    if(that.data.num == 0){
      that.setData({
        codetime: '获取验证码',
        num: 120
      })
    }else{
      that.setData({
        codetime: that.data.num + '   s'
      })
      setTimeout(function(){
        that.setData({
          num: that.data.num - 1
        })
        that.timetid();
      },1000)
    }    
  },
  save: function(){
    var that = this;
    // console.log(that.data.phone,that.data.name,that.data.code)
    if(that.data.phone == '' || that.data.name == '' || that.data.code == '' ){
      wx.showToast({
        title: '请将姓名、手机号、验证码填写完整',
        icon: 'none',
        duration: 3000
      })
    }else{
      wx.request({
        //获取openid接口  
        url: time.httpurl + '/api/saveInfoNew',
        data:{
          "phone": that.data.phone,
          "openId": wx.getStorageSync("openId"),
          "name":that.data.name,
          "code":that.data.code
        },
        method: 'POST',
        success: function (res) {
          console.log(res);
          if(res.data.code == 200){             
            wx.redirectTo({
              url: '../index/index',
            })
          }else{
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 3000
            })
          }
        }
      })
    }
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
    that.setData({
      phone: wx.getStorageSync("phone")
    })
  },
 
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    
  },
 
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
 
  },
 
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
 
  },
 
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
 
  },
 
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
 
  },
 
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
 
  }
})